package MapClass.Trip;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import java.io.IOException;

public class TripStart {
    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
        Configuration conf =  new Configuration();
//        conf.set("fs.defaultFS","hdfs://20210322045-master:9000");

        Job job =Job.getInstance(conf,"排序里程数");
        job.setJarByClass(TripStart.class);                             //运行本身这类
        job.setMapperClass(MyMapper.class);                //指定Map的类
        job.setReducerClass(MyReducer.class); //指定Reducer的类


        job.setMapOutputKeyClass(Tripitem.class);
        job.setMapOutputValueClass(Tripitem.class);

        job.setOutputKeyClass(Tripitem.class);                    //指定map输入key类型
        job.setOutputValueClass(NullWritable.class);  //                    //指定map输出value类型


        FileInputFormat.addInputPath(job, new Path(args[0]));    //输入位置路径
        FileOutputFormat.setOutputPath(job, new Path(args[1]));  //输出位置路径
        System.exit(job.waitForCompletion(true) ? 0 : 1);

    }

    public static class MyMapper extends Mapper<Object, Text, Tripitem, Tripitem> {
        @Override
        protected void map(Object key, Text value, Context context) throws IOException, InterruptedException {
            String[] items = value.toString().split(",");
            Tripitem tripltem = new Tripitem(items[0], Integer.parseInt(items[1]), Integer.parseInt(items[2]));
            context.write(tripltem, tripltem);
        }
    }

    public static class MyReducer extends Reducer<Tripitem, Tripitem, Tripitem, NullWritable> {
        @Override
        protected void reduce(Tripitem key, Iterable<Tripitem> values, Context context) throws IOException, InterruptedException {
            for (Tripitem tripltem : values) {
                context.write(tripltem, NullWritable.get());
            }
        }
    }


}
